<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <el-form class="login-form" :rules="formRules" :model="form" ref="form">
        <el-form-item prop="mobile">
          <el-input placeholder="请输入手机号" v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input placeholder="请输入验证码" v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item prop="checkFlag">
          <el-checkbox v-model="form.checkFlag">我已阅读并同意用户协议和隐私条款</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button class="login-btn" type="primary" :loading="load" @click="totalValidator">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 引入登录请求接口
import {login} from '@/api/user'
// 引入本地存储
import {saveUserInfo} from '@/store'
export default {
  data () {
    //验证验证码函数
    const validator = function(rule,value,callback){
      if(value === ''){
        callback(new Error('验证码不能为空'));
      }
      if(!/^[0-9]{6}$/.test(value)){
        callback(new Error('验证码必须是6位数字'));
      }
      callback();
    }
    //验证协议是否勾选
    const validatorCheck = (rule,value,callback)=>{
      if(!value){
        callback(new Error('请先勾选协议'));
      }
      callback();
    }
    return {
      form: {
        //13911111111
        mobile: '13911111111',
        code: '246810',
        checkFlag: true
      },
      load: false,
      formRules:{
        mobile:[
          {required:true,message:'手机号不能为空',trigger:'blur'},
          {min:11,max:11,message:'手机号长度必须是11位',trigger:'blur'}
        ],
        code:[
          {validator:validator,trigger:'blur'}
        ],
        checkFlag:[
          {validator:validatorCheck,trigger:'change'}
        ]
      }
    }
  },
  methods: {
    // 表单整体验证
    totalValidator(){
      //valid是所有表单项验证通过就为true,否则是false
      this.$refs['form'].validate((valid) => {
          if (valid) {
            // 验证通过,继续登录
            this.toLogin();
          } else {
            return false;
          }
        });
    },
    // 登录
    toLogin () {
      // 一点击按钮，显示加载状态
      this.load = true
      // 发送请求
      login(this.form.mobile,this.form.code).then((res) => {
        console.log(res);
        // 获取token，存储到本地
        saveUserInfo(res.data.data);
        // 提示用户登录成功
        this.$message({
          message: '登录成功',
          type: 'success',
          center: true
        })
        this.load = false
        // 跳转到后台首页
        this.$router.push({
          path:'/'
        });
      }).catch((err) => {
        this.$message({
          message: '登录失败',
          type: 'warning'
        });
        this.load = false;
      })
    }
  }

}
</script>

<style scoped lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("./img/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("./img/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
